package com.alt.base;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alt.manager.ALTAppMgr;
import com.alt.manager.ALTPTCMgr;
import com.alt.manager.ALTPTCMgr.RECEIVER_TYPE;
import com.alt.manager.ALTPTCMgr.SCREEN_UI;
import com.alt.ui.ALTCall;
import com.alt.ui.ALTCall1;
import com.alt.ui.ALTCart;
import com.alt.ui.ALTEditAvartar;
import com.alt.ui.ALTHome;
import com.alt.ui.ALTLoading;
import com.alt.ui.ALTLogin;
import com.alt.ui.ALTMore;
import com.alt.ui.ALTPayment;
import com.alt.ui.ALTRegister;
import com.alt.ui.ALTShareFriend;
import com.alt.ui.ALTTransactionHistory;
import com.alt.ui.ALTViewImage;

public class ALTBaseReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		RECEIVER_TYPE receiveType = (RECEIVER_TYPE) intent.getExtras().getSerializable(ALTPTCMgr.TAG_RECEIVER_TYPE);

		if (receiveType == RECEIVER_TYPE.UI_RECEIVER) {
			processScreenReceive(context, intent);
		} else {
			// processCommandReceive(context, intent);
		}
	}

	private void processScreenReceive(Context context, Intent intent) {
		SCREEN_UI NextScreenMode = (SCREEN_UI) intent.getExtras().getSerializable(ALTPTCMgr.TAG_RECEIVER_NEXT_SCREEN);
		Class<?> cls = null;

		Log.d("ALTBaseReceiver::onReceive", "Screen Mode: [" + NextScreenMode + "] started...");

		boolean startUI = false;
		SCREEN_UI screenTab = SCREEN_UI.HOME;

		switch (NextScreenMode) {
		case LOADING:
			cls = ALTLoading.class;
			break;
		case LOGIN:
			cls = ALTLogin.class;
			break;
		case REGISTER:
			cls = ALTRegister.class;
			break;
		case APP_EXIT:
			break;
		case MAIN:
			screenTab = SCREEN_UI.HOME;
			cls = ALTMainForm.class;
			break;
		case HOME:
			startUI = true;
			screenTab = SCREEN_UI.HOME;
			cls = ALTHome.class;
			break;
		case Personal:
			startUI = true;
			screenTab = SCREEN_UI.Personal;
			cls = ALTCart.class;
			break;			
			
		case SHAREFRIEND:			
			screenTab = SCREEN_UI.Personal;
			cls = ALTShareFriend.class;
			break;
			
		case Exchange:
			startUI = true;
			screenTab = SCREEN_UI.Exchange;
			cls = ALTCall.class;
			break;

		case CALL1:
			screenTab = SCREEN_UI.Setting;
			cls = ALTCall1.class;
			break;

		case Setting:
			startUI = true;
			screenTab = SCREEN_UI.Setting;
			cls = ALTPayment.class;
			break;
			
		case VIEWIMAGE:
			screenTab = SCREEN_UI.HOME;
			cls = ALTViewImage.class;
			break;
			
		case EDITPROFILE:
			screenTab = SCREEN_UI.Personal;
			cls = ALTEditAvartar.class;
			break;
			
		case TRANSACTION_HISTORY:
			screenTab = SCREEN_UI.Personal;
			cls = ALTTransactionHistory.class;
			break;			

		case NONE:
			break;

		default:
			break;
		}

		if (cls == null) {
			return;
		}

		Bundle extras = intent.getBundleExtra(ALTPTCMgr.TAG_RECEIVER_EXTRAS);

		if (cls.getSuperclass().getSimpleName().equals("ALTTabActivity")) {
			startTabActivity(context, cls, extras, screenTab, startUI);
		} else {
			startActivity(context, cls, extras);
		}

		Log.d("ALTBaseReceiver::onReceive", "Screen Mode: [" + NextScreenMode + "] has been called...");
	}

	private void startActivity(Context context, Class<?> cls, Bundle extras) {
		Intent i = new Intent(context, cls);

		if (cls == ALTLoading.class)
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		i.putExtra(ALTPTCMgr.TAG_RECEIVER_EXTRAS, extras);
		PendingIntent sender = PendingIntent.getActivity(context, 0, i, 0);

		try {
			sender.send();
		} catch (CanceledException e) {
			e.printStackTrace();
		}
	}

	private void startTabActivity(Context context, Class<?> cls, Bundle extras, SCREEN_UI screenTab, boolean startUI) {

		ALTAppMgr appMgr = ALTAppMgr.getInstance();

		if (appMgr.m_MainForm == null) {
			return;
		}

		String tabName = ALTMainForm.TAG_TAB_HOME;
		int tabIndex = 0;
		switch (screenTab) {
		case HOME:
			tabIndex = 0;
			tabName = ALTMainForm.TAG_TAB_HOME;
			break;
		case Personal:
			tabIndex = 1;
			tabName = ALTMainForm.TAG_TAB_PERSONAL;
			break;
		case Exchange:
			tabIndex = 2;
			tabName = ALTMainForm.TAG_TAB_EXCHANGE;
			break;
		case Setting:
			tabIndex = 3;
			tabName = ALTMainForm.TAG_TAB_SETTING;
			break;
		default:
			break;
		}

		if (startUI) {
			if (appMgr.m_MainForm.getTabGroup(tabIndex) != null) {
				appMgr.m_MainForm.getTabGroup(tabIndex).popAll();
			}
		}

		Log.d("startTabActivity", "TabName: [" + tabName + "]...");
		appMgr.m_MainForm.setTabChanged(tabIndex);

		String classId = cls.getSimpleName();
		Intent intent = new Intent(context, cls);
		intent.putExtra(ALTPTCMgr.TAG_RECEIVER_EXTRAS, extras);

		appMgr.m_MainForm.getTabGroup(tabIndex).push(classId, intent);
	}
}
